/**
 * Copyright 2010-present Facebook
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.android;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

@SuppressWarnings("deprecation")
public class SessionStore {

	private static final String TOKEN = "access_token";
	private static final String EXPIRES = "expires_in";
	private static final String LAST_UPDATE = "last_update";
	private static final String KEY = "facebook-session";
	private static final String DONT_ASK = "saveDont-AskIfNotLoginFb";

	private static final String DISPLAY_NAME = "save-DISPLAY_NAME";
	private static final String AVATAR_LINK = "save-AVATAR_LINK";

	private static final String REVERT_CHAPTER = "save-REVERT_CHAPTER";

	private static final String SHOW_WARNING = "save-SHOW_WARNING-3";

	public static void saveShowWarning(Context context, boolean enable) {
		Editor editor = context.getSharedPreferences(SHOW_WARNING, Context.MODE_PRIVATE).edit();
		editor.putBoolean(SHOW_WARNING, enable);
		editor.commit();
	}

	public static boolean loadShowWarning(Context context) {
		SharedPreferences savedSession = context.getSharedPreferences(SHOW_WARNING, Context.MODE_PRIVATE);
		return savedSession.getBoolean(SHOW_WARNING, true);
	}

	// =========================================================
	public static void saveRevertChapter(Context context, boolean enable) {
		Editor editor = context.getSharedPreferences(REVERT_CHAPTER, Context.MODE_PRIVATE).edit();
		editor.putBoolean(REVERT_CHAPTER, enable);
		editor.commit();
	}

	public static boolean loadRevertChapter(Context context) {
		SharedPreferences savedSession = context.getSharedPreferences(REVERT_CHAPTER, Context.MODE_PRIVATE);
		return savedSession.getBoolean(REVERT_CHAPTER, false);
	}

	// =========================================================
	public static void saveDontAskIfNotLoginFb(Context context, boolean enable) {
		Editor editor = context.getSharedPreferences(DONT_ASK, Context.MODE_PRIVATE).edit();
		editor.putBoolean(DONT_ASK, enable);
		editor.commit();
	}

	public static boolean loadDontAskIfNotLoginFb(Context context) {
		SharedPreferences savedSession = context.getSharedPreferences(DONT_ASK, Context.MODE_PRIVATE);
		return savedSession.getBoolean(DONT_ASK, false);
	}

	// =========================================================
	public static void saveInfoFaceBook(Context context, String displayname, String avatar) {
		Editor editor = context.getSharedPreferences(DONT_ASK, Context.MODE_PRIVATE).edit();
		editor.putString(DISPLAY_NAME, displayname);
		editor.putString(AVATAR_LINK, avatar);
		editor.commit();
	}

	public static String[] loadInfoFaceBook(Context context) {
		SharedPreferences savedSession = context.getSharedPreferences(DONT_ASK, Context.MODE_PRIVATE);
		String name = savedSession.getString(DISPLAY_NAME, "");
		String avatarlink = savedSession.getString(AVATAR_LINK, "");
		if (name != null && !"".equals(name) && avatarlink != null && !"".equals(avatarlink))
			return new String[] { name, avatarlink };
		return null;
	}

	/*
	 * Save the access token and expiry date so you don't have to fetch it each
	 * time
	 */
	public static boolean save(Facebook session, Context context) {
		Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
		System.out.println("TOKEN " + session.getAccessToken());
		System.out.println("EXPIRES " + session.getAccessExpires());
		System.out.println("LAST_UPDATE " + session.getLastAccessUpdate());

		editor.putString(TOKEN, session.getAccessToken());
		editor.putLong(EXPIRES, session.getAccessExpires());
		editor.putLong(LAST_UPDATE, session.getLastAccessUpdate());
		return editor.commit();
	}

	/*
	 * Restore the access token and the expiry date from the shared preferences.
	 */
	public static boolean restore(Facebook session, Context context) {
		SharedPreferences savedSession = context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
		System.out.println("LOAD TOKEN " + savedSession.getString(TOKEN, null));
		System.out.println("LOAD EXPIRES " + savedSession.getLong(EXPIRES, 0));
		System.out.println("LOAD LAST_UPDATE " + savedSession.getLong(LAST_UPDATE, 0));
		session.setTokenFromCache(savedSession.getString(TOKEN, null), savedSession.getLong(EXPIRES, 0), savedSession.getLong(LAST_UPDATE, 0));
		if (!session.isSessionValid()) {
			System.out.println("TOKEN LOAD AGAIN");
			session.setAccessToken(savedSession.getString(TOKEN, null));
			session.setAccessExpires(savedSession.getLong(EXPIRES, 0));
		}

		return session.isSessionValid();
	}

	public static void clear(Context context) {
		Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
		editor.clear();
		editor.commit();
	}

}
